package com.wanmait.translate.systemTest;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class GetPastAndFetureDate {

    // 转换时间格式（年月日）
    private static String CONVERT_DATE_FORMAT = "yyyy-MM-dd";

    // 转换时间格式（年月日 时分秒 24h制）
    private static String CONVERT_DATE_FORMAT_COMPLETE = "yyyy-MM-dd HH:mm:ss";

    /**
     * 获取过去或者未来任意天内的日期数组（例如获取过去7天的时间列表）
     * @param intervals  intervals天内
     * @return  日期数组 ArrayList<String>
     */
    public static ArrayList<String> GetPastDaysList(int intervals ) {
        ArrayList<String> pastDaysList = new ArrayList<>();
        ArrayList<String> FutureDaysList = new ArrayList<>();
        for (int i = 0; i <intervals; i++) {
            pastDaysList.add(getPastDate(i));
            FutureDaysList.add(getFutureDate(i));
        }
        return pastDaysList;
    }

    /**
     * 获取过去第几天的日期
     * @param past
     * @return String
     */
    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat(CONVERT_DATE_FORMAT);
        String result = format.format(today);
        return result;
    }

    /**
     * 获取未来第几天的日期
     * @param past  几天
     * @return String
     */
    public static String getFutureDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat(CONVERT_DATE_FORMAT);
        String result = format.format(today);
        return result;
    }

    /**
     * 得到某个时间点的几天前的时间
     * @param date  某个时间点
     * @param day  几天
     * @return String
     */
    public static String getDateBefore(String date,int day) {
        Date d = null;
        try {
            d = new SimpleDateFormat(CONVERT_DATE_FORMAT).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)-day);

        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat(CONVERT_DATE_FORMAT);
        String result = format.format(today);
        return result;
    }

    /**
     * 得到某个时间点的几天后的的时间
     * @param date  某个时间点
     * @param day  几天
     * @return
     */
    public static String getAfterBefore(String date,int day) {
        Date d = null;
        try {
            d = new SimpleDateFormat(CONVERT_DATE_FORMAT).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+day);

        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat(CONVERT_DATE_FORMAT);
        String result = format.format(today);
        return result;
    }

}
